<template>
  <div class="mobile-nav-container">
    <!-- 顶部导航栏 -->
    <div class="mobile-nav-header">
      <div class="logo-container" @click="navigateToHome">
        <img src="@/assets/logo.png" alt="HIIT Logo" class="logo" />
        <span class="logo-text">HIIT健身</span>
      </div>
      
      <!-- 菜单按钮 -->
      <el-button
        class="menu-toggle-btn"
        :icon="isMenuOpen ? 'Close' : 'Menu'"
        @click="toggleMenu"
        text
      >
        <el-icon :size="24">
          <component :is="isMenuOpen ? 'Close' : 'Menu'" />
        </el-icon>
      </el-button>
    </div>
    
    <!-- 抽屉菜单 -->
    <el-drawer
      v-model="isMenuOpen"
      direction="ltr"
      size="80%"
      :show-close="false"
      :with-header="false"
      class="mobile-menu-drawer"
    >
      <div class="mobile-menu-content">
        <!-- 用户信息区域 -->
        <div class="user-info" v-if="isLoggedIn">
          <el-avatar :size="50" :src="userAvatar" />
          <span class="username">{{ username }}</span>
        </div>
        
        <!-- 导航菜单 -->
        <div class="mobile-menu-list">
          <div
            v-for="(item, index) in menuItems"
            :key="index"
            class="mobile-menu-item"
            @click="handleMenuClick(item)"
          >
            <el-icon v-if="item.icon" :size="20">
              <component :is="item.icon" />
            </el-icon>
            <span>{{ item.title }}</span>
          </div>
        </div>
        
        <!-- 底部操作区 -->
        <div class="mobile-menu-footer">
          <el-button v-if="isLoggedIn" type="danger" @click="logout" plain>退出登录</el-button>
          <el-button v-else type="primary" @click="login">登录/注册</el-button>
        </div>
      </div>
    </el-drawer>
    
    <!-- 购物车图标 (固定在右下角) -->
    <div class="floating-cart-icon">
      <CartIconNav />
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import { Menu, Close } from '@element-plus/icons-vue';
import CartIconNav from '../icons/CartIconNav.vue';

// 定义组件属性
const props = defineProps({
  // 菜单项配置
  items: {
    type: Array,
    default: () => [
      { title: '首页', path: '/', icon: 'House' },
      { title: '课程', path: '/courses', icon: 'Reading' },
      { title: '我的订单', path: '/orders', icon: 'List' },
      { title: '个人中心', path: '/profile', icon: 'User' }
    ]
  },
  // 用户信息
  isLoggedIn: {
    type: Boolean,
    default: false
  },
  username: {
    type: String,
    default: ''
  },
  userAvatar: {
    type: String,
    default: ''
  }
});

// 定义事件
const emit = defineEmits(['login', 'logout']);

// 路由实例
const router = useRouter();

// 菜单开关状态
const isMenuOpen = ref(false);

// 菜单项
const menuItems = computed(() => props.items);

// 切换菜单
const toggleMenu = () => {
  isMenuOpen.value = !isMenuOpen.value;
};

// 处理菜单点击
const handleMenuClick = (item) => {
  router.push(item.path);
  isMenuOpen.value = false;
};

// 导航到首页
const navigateToHome = () => {
  router.push('/');
  if (isMenuOpen.value) {
    isMenuOpen.value = false;
  }
};

// 登录
const login = () => {
  emit('login');
  isMenuOpen.value = false;
};

// 退出登录
const logout = () => {
  emit('logout');
  isMenuOpen.value = false;
};
</script>

<style scoped>
.mobile-nav-container {
  position: relative;
}

.mobile-nav-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  height: 50px;
}

.logo-container {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.logo {
  height: 30px;
  margin-right: 8px;
}

.logo-text {
  font-size: 18px;
  font-weight: bold;
  color: #409EFF;
}

.menu-toggle-btn {
  padding: 8px;
}

.mobile-menu-drawer :deep(.el-drawer__body) {
  padding: 0;
}

.mobile-menu-content {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.user-info {
  padding: 20px;
  display: flex;
  align-items: center;
  background-color: #f0f2f5;
  border-bottom: 1px solid #e0e0e0;
}

.username {
  margin-left: 15px;
  font-size: 16px;
  font-weight: 500;
}

.mobile-menu-list {
  flex: 1;
  overflow-y: auto;
}

.mobile-menu-item {
  display: flex;
  align-items: center;
  padding: 15px 20px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.mobile-menu-item:hover {
  background-color: #f5f7fa;
}

.mobile-menu-item .el-icon {
  margin-right: 10px;
  color: #409EFF;
}

.mobile-menu-footer {
  padding: 20px;
  border-top: 1px solid #e0e0e0;
  display: flex;
  justify-content: center;
}

.floating-cart-icon {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 900;
  background-color: #fff;
  border-radius: 50%;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  padding: 5px;
}
</style>